#!/usr/bin/python
# -*- encoding: utf-8 -*-
'''
@File    :   7.py
@Time    :   2020/05/04 21:50:15
@Author  :   Malygos_l 
@Version :   1.0
@Contact :   751756061@qq.com
@WebSite :   www.gitee.com/malygos_l
'''
# 使用python代码统计一个文件夹中所有文件的总大小
# Start typing your code from here

import os

def filesize(size):
    if int(size/1024) > 0:
        size = size/1024
        if int(int(size)/1024) > 0:
            return str(size)+" MB"
        else:
            return str(size)+" KB"
    else:
        return str(size)+" B"

def get_size(folder):
    size = 0
    os.chdir(folder)
    print("目录：%s" % os.getcwd())
    if os.path.isdir(os.getcwd()):
        for f in os.walk(os.getcwd()):
            for file in f[2]:
                size += os.path.getsize(os.path.join(f[0],file))
            for folder_son in f[1]:
                get_size(folder_son)
    
    
    return size

def main():
    # size = 0
    txt = "homework4"
    print(filesize(get_size(txt)))

if __name__ == "__main__":
    main()